package org.shagin.jfin.sheet;

import javax.swing.table.AbstractTableModel;

public class SheetTableModel extends AbstractTableModel {

	private static final long serialVersionUID = -8027359667530794528L;

	private Sheet sheet;

	public SheetTableModel(Sheet sheet) {
		this.sheet = sheet;
	}

	@Override
	public int getRowCount() {
		return sheet.getData().getRowCount();
	}

	@Override
	public int getColumnCount() {
		return sheet.getData().getColumnCount();
	}

	@Override
	public String getColumnName(int column) {
		return sheet.getData().getColumn(column).getTitle();
	}

	@Override
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
		SheetRow row = sheet.getData().getRow(rowIndex);
		SheetColumn<?> col = sheet.getData().getColumn(columnIndex);
		row.setCell(col, new SheetCell(aValue));
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		SheetRow row = sheet.getData().getRow(rowIndex);
		SheetColumn<?> col = sheet.getData().getColumn(columnIndex);
		SheetCell cell = row.getCell(col);
		Object result = null;
		if (cell != null)
			result = cell.getObject();
		return result;
	}

	public Sheet getSheet() {
		return sheet;
	}

}
